package com.newsproject.controller;

import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.newsproject.service.SearchService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(tags = "搜索")
@RestController
@RequestMapping("/SearchController")
public class SearchController {

    @Autowired
    private SearchService searchService;

    @ApiOperation(value = "获取搜索结果", notes = "通过搜索字符串获取搜索结果")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "成功"),
            @ApiResponse(code = 400, message = "请求参数错误"),
            @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @GetMapping("/search")
    public ModelAndView searchPage(@ApiParam("搜索字符串") @RequestParam(value = "searchString", required = true) String searchString,HttpSession session) {
        return searchService.searchPage(searchString,session);
    }

    @ApiOperation(value = "获取热搜结果", notes = "获取第Num页的热搜结果（每页10个，按热度排序）")
    @ApiResponses(value = {
        @ApiResponse(code = 200, message = "成功"),
        @ApiResponse(code = 400, message = "请求参数错误"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @GetMapping("/hotSearch/{Num}")
    public ModelAndView hotSearch(@ApiParam("页数") @PathVariable("Num") int Num) {
        return searchService.hotSearch(Num);
    }

    @ApiOperation(value = "获取热搜结果", notes = "(json)获取第Num页的热搜结果（每页10个，按热度排序）")
    @ApiResponses(value = {
        @ApiResponse(code = 200, message = "成功"),
        @ApiResponse(code = 400, message = "请求参数错误"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @GetMapping("/HotSearch/{Num}")
    public Map<String, Object> hotSearchJson(@ApiParam("页数") @PathVariable("Num") int Num) {
        return searchService.HotSearch(Num);
    }

    @ApiResponses(value = {
        @ApiResponse(code = 200, message = "成功"),
        @ApiResponse(code = 400, message = "请求参数错误"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @ApiOperation(value = "获取今日热搜结果", notes = "获取第Num页的热搜结果（每页10个，按热度排序）")
    @GetMapping("/todayHotSearch/{Num}")
    public ModelAndView todayHotSearch(@ApiParam("页数") @PathVariable("Num") int Num) {
        return searchService.todayHotSearch(Num);
    }

    @ApiResponses(value = {
        @ApiResponse(code = 200, message = "成功"),
        @ApiResponse(code = 400, message = "请求参数错误"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @ApiOperation(value = "获取今日热搜结果", notes = "(json)获取第Num页的热搜结果（每页10个，按热度排序）")
    @GetMapping("/TodayHotSearch/{Num}")
    public Map<String, Object> HotSearch(@ApiParam("页数") @PathVariable("Num") int Num) {
        return searchService.searchApi(Num);
    } 

    @ApiResponses(value = {
        @ApiResponse(code = 200, message = "成功"),
        @ApiResponse(code = 400, message = "请求参数错误"),
        @ApiResponse(code = 500, message = "服务器内部错误")
    })
    @ApiOperation(value = "获取搜索结果", notes = "(json)通过搜索字符串获取搜索结果")
    @GetMapping("/Search")
    public Map<String, Object> Search(@ApiParam("搜索字符串") @RequestParam(value = "searchString", required = true) String searchString,HttpSession session) {
        return searchService.search(searchString,session);
    } 
}